package com.sharing.library.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sharing.library.entity.Book;
import com.sharing.library.entity.BookType;
import com.sharing.library.entity.Review;
import com.sharing.library.entity.VO.BookVo;
import com.sharing.library.entity.VO.ReviewVO;
import com.sharing.library.repository.BookRepository;
import com.sharing.library.repository.BookTypeRepository;
import com.sharing.library.repository.dto.BookCondition;
import com.sharing.library.repository.spec.BookSpec;
import com.zhx.framework.util.result.Result;
import com.zhx.framework.util.support.QueryResultConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghaixuan
 * @create 2018/3/17-下午2:03
 **/
@RestController
@RequestMapping("/book")
@Api(description="书管理")
public class BookController {
    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private BookTypeRepository bookTypeRepository;

    @Autowired
    private ObjectMapper objectMapper;

    //详情
    @PostMapping("/detail")
    @ApiOperation(value="详情", notes="详情")
    public Result detail(@RequestParam Long id){
        Book one = bookRepository.findOne(id);

        return Result.OK("",one);
    }

    //列表
    @PostMapping("/list")
    @ApiOperation(value="列表", notes="列表")
    public  Result list (BookCondition condition, Pageable pageable){
        condition.setState("checkIn");
        Page<Book> books = bookRepository.findAll(new BookSpec(condition), pageable);

        List<BookVo> list = new ArrayList<>();

        for(Book book:books){
            BookVo bookVo = objectMapper.convertValue(book, BookVo.class);

            BookType one = bookTypeRepository.findOne(Long.parseLong(bookVo.getTypeId()));

            if(one != null){
                bookVo.setTypeName(one.getTypeName());
            }

            list.add(bookVo);

        }
        PageImpl<BookVo> returnListVo = new PageImpl<BookVo>(list,pageable,books.getTotalElements());

        return Result.OK("",returnListVo);
    }

    //列表
    @PostMapping("/mylist")
    @ApiOperation(value="我的列表", notes="我的列表")
    public  Result mylist (BookCondition condition, Pageable pageable){
        Page<Book> books = bookRepository.findAll(new BookSpec(condition), pageable);

        List<BookVo> list = new ArrayList<>();

        for(Book book:books){
            BookVo bookVo = objectMapper.convertValue(book, BookVo.class);

            BookType one = bookTypeRepository.findOne(Long.parseLong(bookVo.getTypeId()));

            if(one != null){
                bookVo.setTypeName(one.getTypeName());
            }

            list.add(bookVo);

        }
        PageImpl<BookVo> returnListVo = new PageImpl<BookVo>(list,pageable,books.getTotalElements());


        return Result.OK("",returnListVo);
    }


    //上传
    @PostMapping("/save")
    @ApiOperation(value="上传  书的状态  state: none,提交未审核  checkIn 已审核 fail:失败", notes="上传")
    public Result save(@RequestBody Book book){
        book.setState("none");
        Book save = bookRepository.save(book);

        return Result.OK("",save);
    }

}
